package dht.data;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.Vector;

import dht.simulation.GlobalVariables;

public class FileGenerator {

	protected Random ran;

	public FileGenerator() {
		ran = new Random(GlobalVariables.seed);
	}

	public TreeMap<Integer, String> generate(int number) throws IOException {
		TreeMap<Integer, String> res = new TreeMap<Integer, String>();
		Scanner cin = new Scanner(new File("data/firstName.txt"));
		Vector<String> firstName = new Vector<String>();
		while (cin.hasNext()) {
			firstName.add(cin.next());
		}
		cin = new Scanner(new File("data/lastName.txt"));
		Vector<String> lastName = new Vector<String>();
		while (cin.hasNext()) {
			lastName.add(cin.next());
		}
		cin.close();
		HashSet<String> fileNames = new HashSet<String>();
		for (int i = 0; i < number; ++i) {
			String fileName = "";
			do {
				fileName = firstName.get(ran.nextInt(firstName.size()))
						+ lastName.get(ran.nextInt(lastName.size()));
			} while (fileNames.contains(fileName));
			fileNames.add(fileName);
		}
		Vector<Integer> rank = new Vector<Integer>();
		for (int i = 1; i <= number; ++i) {
			rank.add(i);
		}
		for (int i = 0; i < number; ++i) {
			int n = ran.nextInt(number);
			int tmp = rank.get(n);
			rank.set(n, rank.get(i));
			rank.set(i, tmp);
		}
		int i = 0;
		for (String key : fileNames) {
			res.put(rank.get(i), key);
			i++;
		}
		return res;
	}

	public void generate(int number, String outputName) throws IOException {
		PrintWriter pw = new PrintWriter(outputName);
		Scanner cin = new Scanner(new File("data/firstName.txt"));
		Vector<String> firstName = new Vector<String>();
		while (cin.hasNext()) {
			firstName.add(cin.next());
		}
		cin = new Scanner(new File("data/lastName.txt"));
		Vector<String> lastName = new Vector<String>();
		while (cin.hasNext()) {
			lastName.add(cin.next());
		}
		cin.close();
		Vector<Integer> rank = new Vector<Integer>();
		for (int i = 1; i <= number; ++i) {
			rank.add(i);
		}
		for (int i = 0; i < number; ++i) {
			int n = ran.nextInt(number);
			int tmp = rank.get(n);
			rank.set(n, rank.get(i));
			rank.set(i, tmp);
		}

		HashSet<String> fileNames = new HashSet<String>();
		for (int i = 0; i < number; ++i) {
			String fileName = "";
			do {
				fileName = firstName.get(ran.nextInt(firstName.size()))
						+ lastName.get(ran.nextInt(lastName.size()));
			} while (fileNames.contains(fileName));
			fileNames.add(fileName);
		}
		int i = 0;
		for (String key : fileNames) {
			pw.println(rank.get(i) + " " + key);
			// res.put(rank.get(i), key);
			i++;
		}
		pw.close();
		// return res;
	}

	public static void main(String[] args) throws IOException {
		FileGenerator gen = new FileGenerator();
		TreeMap<Integer, String> files = gen.generate(10);
		for (Integer key : files.keySet()) {
			System.out.println(key + " " + files.get(key));
		}
	}

}
